package top.jolyoulu.codegenweb.service.bll.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.jolyoulu.codegenweb.entity.bo.query.GenCodeTaskQueryBO;
import top.jolyoulu.codegenweb.entity.po.GenCodeTask;
import top.jolyoulu.codegenweb.entity.vo.DataSourceInfoVO;
import top.jolyoulu.codegenweb.entity.vo.GenCodeTaskVO;
import top.jolyoulu.codegenweb.service.bll.ApiDataSourceService;
import top.jolyoulu.codegenweb.service.bll.ApiGenCodeTaskService;
import top.jolyoulu.codegenweb.service.dal.GenCodeTaskService;
import top.jolyoulu.corecommon.utils.io.FileUtils;
import top.jolyoulu.modules.mybatisplusmodule.plugin.page.JlPage;
import top.jolyoulu.webcommon.utils.ResponseUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;

/**
 * @Author: Jolyoulu
 */
@Slf4j
@Service
public class ApiGenCodeTaskServiceImpl implements ApiGenCodeTaskService {

    @Autowired
    private GenCodeTaskService genCodeTaskService;

    @Autowired
    private ApiDataSourceService apiDataSourceService;

    @Override
    public JlPage<GenCodeTaskVO> pageList(GenCodeTaskQueryBO bo) {
        JlPage<GenCodeTaskVO> res = JlPage.execute(
                bo.getPage(), bo.getPageSize(),
                () -> {
                    LambdaQueryWrapper<GenCodeTask> ew = new LambdaQueryWrapper<>();
                    List<GenCodeTask> list = genCodeTaskService.list(ew);
                    return list.stream().map(item -> {
                        GenCodeTaskVO vo = new GenCodeTaskVO(item);
                        DataSourceInfoVO dataSource = apiDataSourceService.getById(item.getDataSourceId());
                        vo.setDataSourceInfoVO(dataSource);
                        return vo;
                    }).collect(Collectors.toList());
                });
        return res;
    }

    @Override
    public GenCodeTaskVO getById(String id) {
        GenCodeTask res = genCodeTaskService.getById(id);
        return new GenCodeTaskVO(res);
    }

    @Override
    @Transactional
    public void delByIds(List<String> ids) {
        for (String id : ids) {
            genCodeTaskService.removeById(id);
        }
    }

    @Override
    public void downloadZip(String id, HttpServletResponse response) {
        GenCodeTask res = genCodeTaskService.getById(id);
        String sourceFolderPath = res.getFilePath();
        try {
            ResponseUtils.outZip(response, res.getId(), zipOutputStream -> {
                File sourceFolder = new File(sourceFolderPath);
                try {
                    FileUtils.zipFile(sourceFolder, sourceFolder.getName(), zipOutputStream);
                } catch (IOException e) {
                    log.error("文件压缩失败", e);
                }
                log.info("文件夹已成功压缩为：" + sourceFolder.getPath() + ".zip");
            });
        } catch (IOException e) {
            log.error("文件压缩失败", e);
        }

    }


}